﻿#region Copyright

//  ======================================================
//      Copyright (c) 2011 Toulr All rights reserved.
//     
//      The use and distribution terms for this software are contained in the file
//      named license.txt, which can be found in the root of this distribution.
//      By using this software in any fashion, you are agreeing to be bound by the
//      terms of this license.
//     
//     You must not remove this notice, or any other, from this software.
//  ======================================================

#endregion

#region

using System;
using System.Data.Entity;
using System.Diagnostics;
using System.Threading;
using NUnit.Framework;
using Toulr.Domains;
using Toulr.Domains.Account;
using Toulr.Domains.Business;

#endregion

namespace Toulr.Test.Data
{
    /// <summary>
    ///   通过测试店铺、商品、团购三类数据的访问和获取，测试EF框架效率、并发访问等问题
    /// </summary>
    [TestFixture]
    public class CodeFirstFixture
    {
        #region Setup/Teardown

        private Shop s;
        private User user;
        private Goods goods;
        private Promotions p;
        DomainContext domainContext = DomainContext.Instance;
        [SetUp]
        public void Init ( )
        {
            Database.SetInitializer (new DropCreateDatabaseAlways <DomainContext> ());
            
             s =
                domainContext.Shops.Add(new Shop { ShopName = "name ", JoinTime = DateTime.Now, CertifiedTime = DateTime.Now });
             user =
                domainContext.Users.Add(new User
                {
                    BirthDay = 1,
                    BirthMonth = 1,
                    BirthPlace = "beijing",
                    BirthYear = 1982,
                    Email = "hh@ss.com",
                    NickName = "hhle",
                    EmailVerified = true,
                    FullName = "test",
                    Identitied = true,
                    RegisterTime = DateTime.Now,
                    SaltValue = 111
                });
             goods = domainContext.Goodses.Add(new Goods { GoodName = "test", ShopId = s.Id, OriginalPrice = 12.0f });
             p =
                domainContext.Promotionses.Add(new Promotions
                {
                    Title = "hhe",
                    GoodsId = goods.Id,
                    StartTime = DateTime.Now,
                    EndTime = DateTime.Now,
                    Confirmed = true,

                });
            domainContext.SaveChanges();
        }

        [TearDown]
        public void Dispose ( )
        {
            //删除初始化的数据
        }

        #endregion

        public DomainContext DbContext = DomainContext.Instance;

        private void InitTestData ( )
        {
            lock (domainContext)
            {
                for (int i = 1; i < 1000; i++)
                {
                    domainContext.Orders.Add(new Order
                    {
                        BillUserId = user.Uid,
                        DoneTime = DateTime.Now,
                        GoodId = goods.Id,
                        GoodTitle = goods.GoodName,
                        TurnoverTime = DateTime.Now,
                        ShopId = s.Id,
                        BuyCount = 1
                    });
                    domainContext.SaveChanges();
                }
            }
        }

        [Test]
        public void ConcurrentAccessEntities ( )
        {
            //模拟开放4个线程测试并发进行数据读写
            var thread = new Thread (InitTestData);
            thread.Start ();
            thread = new Thread(InitTestData);
            thread.Start();
            while (true)
            {
                Thread.CurrentThread.Join(1000);
            } 
        }

        [Test]
        public void EnitiesUpdateInOneContextText ( )
        {
            Debug.Print ("测试使用EF更新对象");


            Debug.Print ("结束测试");
        }
    }
}
